package com.putong35.common.customviews.edittext;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.putong35.common.R;
import com.putong35.common.customviews.RichEditTextBar;
import com.putong35.common.utils.DimensionUtil;

/**
 * Created by Zoi.
 * E-mail：KyluZoi@gmail.com
 * 2016/6/29
 */
public class PasswordEditTextBar extends RichEditTextBar implements View.OnClickListener {

    private ImageView mIvContentleft;
    private ImageView mIvContentRight;
    private LinearLayout mLytImages;
    private boolean isHide = true;
    private int mShowSrc;
    private int mHideSrc;

    public PasswordEditTextBar(Context context) {
        super(context, null);

    }

    public PasswordEditTextBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        setupViews(attrs);
    }

    private void setupViews(AttributeSet attrs) {
        mLytImages = new LinearLayout(getContext());
        mIvContentleft = new ImageView(getContext());
        mIvContentRight = new ImageView(getContext());
        mIvContentleft.setOnClickListener(this);
        mIvContentRight.setOnClickListener(this);
        setLinearImagesinit(mLytImages);
        LayoutParams subImageslytParams = new LayoutParams(DimensionUtil.dip2px(getContext(), 60), LayoutParams.WRAP_CONTENT);
        if (attrs != null) {
            TypedArray tpa = getContext().obtainStyledAttributes(attrs, R.styleable.PasswordEditTextBar);
            float width = tpa.getDimension(R.styleable.PasswordEditTextBar_imagesWidth, -1);
            if (width != -1) {
                subImageslytParams = new LayoutParams((int) width, LayoutParams.WRAP_CONTENT, 1);
            }

            int tempSrc = tpa.getResourceId(R.styleable.PasswordEditTextBar_dismissSrc, -1);
            if (tempSrc != -1) {
                mHideSrc = tempSrc;
                mIvContentleft.setImageResource(mHideSrc);
            }
            tempSrc = tpa.getResourceId(R.styleable.PasswordEditTextBar_displaySrc, -1);
            if (tempSrc != -1) {
                mShowSrc = tempSrc;
            }

            int rightSrc = tpa.getResourceId(R.styleable.PasswordEditTextBar_deleteSrc, -1);
            if (rightSrc != -1) {
                mIvContentRight.setImageResource(rightSrc);
            }
            boolean displayVisi = tpa.getBoolean(R.styleable.PasswordEditTextBar_displayVisible, true);
            if (!displayVisi) {
                mIvContentleft.setVisibility(GONE);
            }
            boolean deleteVisi = tpa.getBoolean(R.styleable.PasswordEditTextBar_deleteVisible, true);
            if (!deleteVisi) {
                mIvContentRight.setVisibility(GONE);
            }
            isHide = tpa.getBoolean(R.styleable.PasswordEditTextBar_isPassword, true);
            if (isHide) {
                showEditContent(isHide);
            }
            tpa.recycle();
        }
        addExtraView(mLytImages, subImageslytParams, Gravity.CENTER_VERTICAL | Gravity.RIGHT);

    }

    private void setLinearImagesinit(LinearLayout linearLayout) {

        linearLayout.setOrientation(HORIZONTAL);
        LayoutParams subImageParams = new LayoutParams(0, LayoutParams.MATCH_PARENT, 1.0f);
        mHideSrc = R.mipmap.ic_launcher;
        mShowSrc = mHideSrc;
        mIvContentRight.setImageResource(mHideSrc);
        mIvContentleft.setImageResource(R.mipmap.ic_launcher);
        linearLayout.addView(mIvContentleft, subImageParams);
        linearLayout.addView(mIvContentRight, subImageParams);


    }

    @Override
    public void onClick(View v) {
        if (v.hashCode() == mIvContentRight.hashCode()) {
            clearEditContent();
        } else if (v.hashCode() == mIvContentleft.hashCode()) {
            isHide = !isHide;
            if (isHide) {
                mIvContentleft.setImageResource(mHideSrc);
            } else {
                mIvContentleft.setImageResource(mShowSrc);
            }
            showEditContent(isHide);
        }
    }


    public CharSequence getPassword() {
        return mEtContent.getText();
    }

}
